﻿using Microsoft.Extensions.DependencyInjection;
using RuoVea.EventBus;
using RuoVea.ExApp;
using System.Threading;

namespace RuoVea.ExEvenBus;

/// <summary>
/// 事件总线 单机版本 若要分布式版本可参考 CAP 
/// </summary>
public class Emit
{
    /// <summary>
    /// 事件发布
    /// </summary>
    /// <param name="eventId">事件 Id</param>
    public static void Publish(string eventId)
    {
        IEventPublisher _eventPublisher = App.GetRequiredService<IEventPublisher>();
        _eventPublisher.PublishAsync(new ChannelEventSource(eventId));
    }

    /// <summary>
    /// 事件发布
    /// </summary>
    /// <param name="eventId">事件 Id</param>
    /// <param name="payload">事件承载（携带）数据</param>
    public static void Publish(string eventId, object payload)
    {
        IEventPublisher _eventPublisher = App.GetRequiredService<IEventPublisher>();
        _eventPublisher.PublishAsync(new ChannelEventSource(eventId, payload));
    }

    /// <summary>
    /// 事件发布
    /// </summary>
    /// <param name="eventId">事件 Id</param>
    /// <param name="payload">事件承载（携带）数据</param>
    /// <param name="cancellationToken">取消任务 Token</param>
    public static void Publish(string eventId, object payload, CancellationToken cancellationToken)
    {
        IEventPublisher _eventPublisher = App.GetRequiredService<IEventPublisher>();
        _eventPublisher.PublishAsync(new ChannelEventSource(eventId, payload, cancellationToken));
    }
}
